"""备注 API Router"""
from typing_extensions import Annotated

from fastapi import APIRouter, Depends

from api.v1.service.point import PointServiceAPI
from core.models import Member, Memo
from lib.common import get_paging_info

from api.v1.dependencies.member import get_current_member
from api.v1.dependencies.memo import get_memo, validate_send_memo
from api.v1.service.memo import MemoServiceAPI
from api.v1.models.response import (
    MessageResponse, response_401, response_403, response_422, response_404
)
from api.v1.models.memo import MemoResponse, SendMemo, MemoList, MemoListResponse

router = APIRouter()


@router.get("/memos",
            summary="票据查询清单",
            responses={**response_401, **response_422})
async def read_memos(
    service: Annotated[MemoServiceAPI, Depends()],
    member: Annotated[Member, Depends(get_current_member)],
    data: Annotated[MemoList, Depends()]
) -> MemoListResponse:
    """Lup当前登录成员的列表."""
    total_records = service.fetch_total_records(data.me_type, member.mb_id)
    paging_info = get_paging_info(data.page, data.per_page, total_records)
    memos = service.fetch_memos(data.me_type, member.mb_id,
                                     data.offset, data.per_page)

    return {
        "total_records": total_records,
        "total_pages": paging_info["total_pages"],
        "memos": memos
    }

@router.get("/memos/{me_id}",
            summary="备注查询",
            responses={**response_401, **response_403, **response_404, **response_422})
async def read_member_memo(
    service: Annotated[MemoServiceAPI, Depends()],
    member: Annotated[Member, Depends(get_current_member)],
    memo: Annotated[Memo, Depends(get_memo)],
) -> MemoResponse:
    """
    查询1条纸条。
    -只能查询自己的纸条。
    """
    prev_memo, next_memo = service.fetch_prev_next_qa(memo.me_id, member)

    return {
        "memo": memo,
        "prev_memo": prev_memo,
        "next_memo": next_memo,
    }


@router.patch("/memos/{me_id}/read",
              summary="阅读笔记的处理",
              responses={**response_401, **response_403, **response_404, **response_422})
async def update_read_member_memo(
    service: Annotated[MemoServiceAPI, Depends()],
    member: Annotated[Member, Depends(get_current_member)],
    memo: Annotated[Memo, Depends(get_memo)],
) -> MessageResponse:
    """
    阅读笔记.
    - 你只能读你自己的笔记。.
    """
    service.update_read_datetime(memo)
    service.update_not_read_memos(member)

    return {"message": "纸条被读了。."}


@router.post("/memos",
             summary="发送便笺",
             responses={**response_403, **response_404, **response_422})
async def send_memo(
    service: Annotated[MemoServiceAPI, Depends()],
    point_service: Annotated[PointServiceAPI, Depends()],
    member: Annotated[Member, Depends(get_current_member)],
    data: Annotated[SendMemo, Depends(validate_send_memo)]
) -> MessageResponse:
    """
    我寄了一张纸条.
    - 点用完，当你发送一个便笺.

    ### Request Body
    - **me_recv_mb_id**: 会员收到便条的Idy (,它可以传播给多个人，由)
    - **me_memo**: 注释的内容
    """
    # 向目标成员发送备注
    send_point = point_service.get_config_point("cf_memo_send_point")

    for target in data.members:
        memo = service.send_memo(member, target, data.me_memo)
        service.update_memo_call(member, target)
        # 点唐纳
        point_service.save_point(
            member.mb_id, send_point * (-1),
            f"{target.mb_nick}({target.mb_id})给你发个纸条", "@memo",
            target.mb_id, memo.me_id)

    return {"message": "我发了一张纸条。."}


@router.delete("/memos/{me_id}",
               summary="删除便笺",
               responses={**response_403, **response_404})
async def delete_member_memo(
    memo_service: Annotated[MemoServiceAPI, Depends()],
    member: Annotated[Member, Depends(get_current_member)],
    memo: Annotated[Memo, Depends(get_memo)],
) -> MessageResponse:
    """
    删除备注.

    ### 一起处理的工作
    - 删除附注通知
    - 续订未读笔记的数量
    """
    memo_service.delete_memo_call(memo)
    memo_service.delete_memo(memo)
    memo_service.update_not_read_memos(member)

    return {"message": "你删除了笔记。."}
